import { defineEventHandler, getQuery } from 'h3'

export default defineEventHandler(async (event) => {
  const query = getQuery(event)
  const path = query.path as string
  const token = query.token as string

  if (token !== 'xu') {
    return { success: false, message: '非法请求' }
  }

  if (!path) {
    return { success: false, message: '缺少 path 参数' }
  }

  try {
    // 调用 Nitro 的 revalidate 方法刷新指定路径
    await event.node.res?.revalidate?.(path) // Nuxt 4 >= 4.4 可用

    return { success: true, path }
  } catch (err) {
    return { success: false, error: (err as Error).message }
  }
})
